#include "configmodel.h"

ConfigModel::ConfigModel(QObject *parent)
    : QAbstractListModel(parent) {}

ConfigModel::~ConfigModel() {}

int ConfigModel::rowCount(const QModelIndex &parent) const {
    Q_UNUSED(parent);
    return items.size();
}

QVariant ConfigModel::data(const QModelIndex &index, int role) const {
    if (!index.isValid() || index.row() >= items.size())
        return QVariant();

    const QVariantMap &item = items.at(index.row());
    switch (role) {
    case KeyRole: return item["key"];
    case ValueRole: return item["value"];
    case TypeRole: return item["type"];
    case DescriptionRole: return item["description"];
    case IOTypeRole: return item["IOType"];
    case NameRole: return item["name"];
    case ValueMaxRole: return item["valueMax"];
    case ValueMinRole: return item["valueMin"];
    case QmlstyleRole: return item["qmlstyle"];
    case OptionRole: return item["option"];
    default: return QVariant();
    }
}

bool ConfigModel::setData(const QModelIndex &index, const QVariant &value, int role) {
    if (role != ValueRole || !index.isValid())
        return false;

    QVariantMap &item = items[index.row()];
    item["value"] = value;

    LogDebug << "Attempting to set data for " << item["key"].toString() << " to " << value.toString();

    try {
        ConfigManager::getInstance().setParameter(currentCategory, item["key"].toString() ,value);
        emit dataChanged(index, index, {role});
        LogInfo << "Data set successfully for " << item["key"].toString();
        return true;
    } catch (const std::exception& e) {
        LogError << "Failed to set data: " << e.what();
        return false;
    }
}

QHash<int, QByteArray> ConfigModel::roleNames() const {
    QHash<int, QByteArray> roles;
    roles[KeyRole] = "key";
    roles[ValueRole] = "value";
    roles[TypeRole] = "type";
    roles[DescriptionRole] = "description";
    roles[IOTypeRole] = "IOType";
    roles[NameRole] = "name";
    roles[ValueMaxRole] = "valueMax";
    roles[ValueMinRole] = "valueMin";
    roles[QmlstyleRole] = "qmlstyle";
    roles[OptionRole] = "option";
    return roles;
}

void ConfigModel::loadData(const QString& category) {
    LogInfo << "Loading data for category: " << category;
    currentCategory = category;
    items.clear();  // 清空现有数据

    try {
        QVariantMap params = ConfigManager::getInstance().getAllParametersByCategory(category.toStdString());
        if (params.isEmpty()) {
            LogWarning << "No parameters found for category: " << category;
            return;
        }

        for (auto iter = params.begin(); iter != params.end(); ++iter) {
            QVariantMap item;
            item["key"] = iter.key();
            item["value"] = iter.value().toMap()["value"];
            item["type"] = iter.value().toMap()["type"];
            item["description"] = iter.value().toMap()["description"];
            item["IOType"] = iter.value().toMap()["IOType"];
            item["valueMax"] = iter.value().toMap()["valueMax"];
            item["valueMin"] = iter.value().toMap()["valueMin"];
            item["qmlstyle"] = iter.value().toMap()["qmlstyle"];
            item["name"] = iter.value().toMap()["name"];
            item["option"] = iter.value().toMap()["option"];
            items.append(item);
        }

        LogInfo << "Data loaded successfully for category: " << category;
        LogDebug << "Data loaded successfully for items: " << items;
    } catch (const std::exception& e) {
        LogError << "Error loading data for category: " << category << " - " << e.what();
    }

    emit dataChanged(createIndex(0, 0), createIndex(items.size() - 1, 0));
}


void ConfigModel::saveData() {
    LogInfo << "Saving data for category: " << currentCategory;

    try {
        for (const QVariantMap& item : items) {
            QVariant key = item["key"];
            QVariant value = item["value"];
            ConfigManager::getInstance().setParameter(currentCategory, key.toString(), value);

        }

        LogInfo << "Data saved successfully for category: " << currentCategory;
    } catch (const std::exception& e) {
        LogError << "Error saving data for category: " << currentCategory << " - " << e.what();
    }
}

